###########################################################################
# $Id: ConceptInstances.p 43 2009-07-13 10:07:20Z donbeave $
###########################################################################

@CLASS
ConceptInstances

@USE
pf/web/pfSiteModule.p

@BASE
pfSiteModule

@OPTIONS
locals

#----- Constructor -----

@create[hshOptions]
  ^BASE:create[$hshOptions]

  ^if($hshOptions.concept is conceptModule){
    $self._concept[$hshOptions.concept]
  }{
     ^pfAssert:fail[Concept object is wrong [$hshOptions.concept.CLASS_NAME].]
   }

  $self._responseType[json]

  ^router.assign[objects/:ID;objects][
    $.requirements[
      $.ID[\d+]
    ]
  ]

  ^router.assign[objects/:ParentID/children;objects][
    $.requirements[
      $.ParentID[\d+]
    ]
  ]

  ^router.assign[objects/:ParentID/add;objects/add][
    $.requirements[
      $.ParentID[\d+]
    ]
  ]

  ^router.assign[objects/:ID/update;objects/update][
    $.requirements[
      $.ID[\d+]
    ]
  ]

  ^router.assign[objects/:ID/remove;objects/remove][
    $.requirements[
      $.ID[\d+]
    ]
  ]

  ^router.assign[objects/:ObjectID/blocks;blocks][
    $.requirements[
      $.ObjectID[\d+]
    ]
  ]

#----- Events -----

@postJSON[hshResponse]
  $result[
    $.type[text]
    $.content-type[application/json]
    $.body[^render[concept.objectsList.^_responseType.upper[].pt]]]
  ]

@onInit[objAction]
  $oLocales[^_getLocales[]]
  $oObjectsTypes[^_getObjectsTypes[]]
  $oDataTypes[^_getDataTypes[]]

  ^_return[
    $.locales[
      $.data[$oLocales]
    ]
    $.objectsTypes[
      $.data[$oObjectsTypes]
    ]
    $.dataTypes[
      $.data[$oDataTypes]
    ]
  ][
    $.name[init]
    $.type[get]
    $.status(0)
  ]

@onObjects[objAction]
  ^switch(true){
    ^case(def $objAction.ID){
      $oItem[^_getObjectByID(^objAction.ID.int(0))]

      ^_return[$oItem;
        $.name[showObjectByID]
        $.type[get]
        $.status(0)
      ]
    }
    ^case(def $objAction.ParentID){
      $oChildren[^_getObjectChildsByObjectID(^objAction.ParentID.int(0))]

      ^_return[$oChildren;
        $.name[showObjectChilds]
        $.type[get]
        $.status(0)
      ]
    }
    ^case[DEFAULT]{
      $oObjects[^_getRootObjects[]]

      ^_return[$oObjects;
        $.name[showRootObjects]
        $.type[get]
        $.status(0)
      ]
    }
  }

@onObjectsAdd[objAction]
  ^_initFormManager[]

  $oObjectItem[^_createObjectFromForm[]]

  ^if(^objAction.ParentID.int(0)){
    $oObjectItem.parent[^STORAGE.getObjectByID(^objAction.ParentID.int(0))]

    $sActionName[addObjectChild]
  }

  ^oObjectItem.save[]

  $oList[^pfArrayList::create[$oObjectItem]]
  ^oList.add[$oObjectItem]

  ^_return[$oList;
    $.name[^if(def $sActionName){$sActionName}{addObject}]
    $.type[post]
    $.status(0)
  ]

@onObjectsUpdate[objAction]
  $oItem[^_getObjectByID(^objAction.ID.int(0))]

  ^_initFormManager[]
  $hItem[^hash::create[$labourFormManager:form.item]]
  $hTexts[^hash::create[$labourFormManager:form.textItems]]

  ^hItem.foreach[sName;aData]{
    ^oItem.set[$sName;$aData]
  }

  ^if(^hItem.ParentID.int(0)){
    $oItem.parent[^STORAGE.getObjectByID(^hItem.ParentID.int(0))]
  }

  ^if($hTexts){
    ^hTexts.foreach[iLocaleID;hData]{
      $oLocale[^STORAGE.getLocaleByID[$iLocaleID]]
      ^oItem.setData[$hData;$oLocale]
    }
  }

# сохраняет данные в БД
  ^oItem.save[]

  $oList[^pfArrayList::create[$oItem]]
  ^oList.add[$oItem]

  ^_return[$oList;
    $.name[updateObject]
    $.type[post]
    $.status(0)
  ]

@onObjectsRemove[objAction]
  $oItem[^_getObjectByID(^objAction.ID.int(0))]

  ^oItem.remove[]

  $oList[^pfArrayList::create[$oItem]]
  ^oList.add[$oItem]

  ^_return[$oList;
    $.name[removeObject]
    $.type[post]
    $.status(0)
  ]

@onObjectsRoot[objAction]
  $result[^onObjects[$objAction]]

@onBlocks[objAction]
  ^switch(true){
    ^case(def $objAction.ObjectID){
      $oBlocks[^_getBlocksByObjectID(^objAction.ObjectID.int(0))]

      ^_return[$oBlocks;
        $.name[showObjectBlocks]
        $.type[get]
        $.status(0)
      ]
    }
  }

#----- Private -----

@_return[altData;hshAct]
  $caller.result[
    $.body[
      $.action[$hshAct]
      $.data[$altData]
    ]
  ]

  ^assignVar[body;$caller.result.body]
  ^assignVar[action;$caller.result.body.action]
  ^assignVar[data;$caller.result.body.data]

@_createObjectFromForm[]
  $hItem[^hash::create[$labourFormManager:form.item]]
  $hTexts[^hash::create[$labourFormManager:form.textItems]]

  ^pfAssert:isTrue(^hItem.TypeID.int(0))[Object type's id not specified or wrong (CLASS_NAME: $hItem.TypeID.CLASS_NAME).]

  $oType[^STORAGE.getObjectTypeByID[$hItem.TypeID]]

  $result[^STORAGE.createObject[$oType]]

  ^if($hTexts){
    ^hTexts.foreach[iLocaleID;hData]{
      $oLocale[^STORAGE.getLocaleByID[$iLocaleID]]

      ^result.setLocalizedData[$hData;$oLocale]
    }
  }

@_getLocales[]
  $result[^STORAGE.getLocales[]]

@_getObjectsTypes[]
  $result[^STORAGE.getObjectsTypes[]]

@_getDataTypes[]
  $result[^STORAGE.getDataTypes[]]

@_getRootObjects[]
  $result[^STORAGE.getRootObjects[]]

@_getObjectByID[intID]
  $result[^STORAGE.getObjectByID($intID)]

@_getObjectChildsByObjectID[intParentID]
  $oItem[^STORAGE.getObjectByID[$intParentID]]
  $result[$oItem.children]

@_getBlocksByObjectID[intObjectID]
  $result[^STORAGE.getBlocksByObjectID($intObjectID)]

@_initFormManager[]
  ^use[labour/web/forms/labourFormManager.p]

#----- Properties -----

@GET_CONCEPT[]
  $result[$self._concept]

@GET_STORAGE[]
  $result[$self.CONCEPT.STORAGE]

@GET_DATA_MANAGER[]
  $result[$self.CONCEPT.DATA_MANAGER]